package com.go.where.manager.action.go;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.CourseInfo;
import com.go.where.manager.pojo.TeacherInfo;
import com.go.where.manager.pojo.TopicInfo;
import com.go.where.manager.service.CourseService;
import com.go.where.manager.service.TeacherService;
import com.go.where.manager.service.TopicService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.KindEditUtil;

@InterceptorRefs( {
		@InterceptorRef(value = "defaultStack", params = { "maximumSize",
				"2048000", "allowedTypes",
				"image/bmp,image/png,image/gif,image/jpeg" }),
		@InterceptorRef(value = "defalutStackSet") })
public class CourseAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4383195694100314090L;
	private static final Logger log = Logger.getLogger(CourseAction.class);

	public Pagination<?> page;

	@Resource(name = "courseService")
	private CourseService courseService;
	@Resource(name = "teacherService")
	private TeacherService teacherService;
	@Resource(name = "topicService")
	private TopicService topicService;

	private CourseInfo courseInfo;
	private CourseInfo courseInfoQuery;

	private List<TeacherInfo> teacherList;
	private List<TopicInfo> topicList;

	private String courseId;
	private String courseSubId;

	private File coursePhoto;// 上传的文件
	private String coursePhotoFileName; // 文件名称
	private String coursePhotoContentType; // 文件类型

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (courseInfoQuery == null) {
			courseInfoQuery = new CourseInfo();
			courseInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			courseInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			courseInfoQuery.setIsDelete(Constants.NOT_DELETED);
			page = courseService.queryPage(courseInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	public String toEdit() {
		courseInfo = courseService.queryByPrimartKey(courseId);
		topicList = topicService.queryAll();
		teacherList = teacherService.queryAll();
		return INPUT;
	}

	public String add() {
		courseSubId = courseService.getCourseSubId();
		topicList = topicService.queryAll();
		teacherList = teacherService.queryAll();
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			String headerRealpath = ServletActionContext.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR
									+ Constants.COURSE_PHOTO_IMAGES);
			String couPhoto = KindEditUtil.uploadPicture(coursePhoto,
					coursePhotoFileName, headerRealpath,
					Constants.COURSE_PHOTO_IMAGES);
			courseInfo.setCoursePhoto(couPhoto);
			courseService.save(courseInfo, getCurrentOperator());
			redirectionUrl = "course!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！" + e);
			return this.add();
		}
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {
		try {
			if (coursePhoto != null) {
				String headerRealpath = ServletActionContext
						.getServletContext().getRealPath(
								CommonUtil.FILE_SEPARATOR
										+ Constants.COURSE_PHOTO_IMAGES);
				String couPhoto = KindEditUtil.uploadPicture(coursePhoto,
						coursePhotoFileName, headerRealpath,
						Constants.COURSE_PHOTO_IMAGES);
				courseInfo.setCoursePhoto(couPhoto);
			}
			courseService.update(courseInfo, getCurrentOperator());
			redirectionUrl = "course!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！" + e);
			courseId = courseInfo.getCourseId();
			return this.toEdit();
		}
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	public String detail() {
		topicList = topicService.queryAll();
		teacherList = teacherService.queryAll();
		courseInfo = courseService.queryByPrimartKey(courseId);
		return "detail";
	}

	/**
	 * 假删除
	 * 
	 * @return
	 */
	public String falseDelete() {
		courseInfo = new CourseInfo();
		courseInfo.setCourseId(courseId);
		courseInfo.setIsDelete(Constants.DELETED);
		try {
			courseService.update(courseInfo, getCurrentOperator());
			redirectionUrl = "course!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常" + e);
			return this.list();
		}
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public List<TeacherInfo> getTeacherList() {
		return teacherList;
	}

	public void setTeacherList(List<TeacherInfo> teacherList) {
		this.teacherList = teacherList;
	}

	public CourseInfo getCourseInfo() {
		return courseInfo;
	}

	public void setCourseInfo(CourseInfo courseInfo) {
		this.courseInfo = courseInfo;
	}

	public CourseInfo getCourseInfoQuery() {
		return courseInfoQuery;
	}

	public void setCourseInfoQuery(CourseInfo courseInfoQuery) {
		this.courseInfoQuery = courseInfoQuery;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public List<TopicInfo> getTopicList() {
		return topicList;
	}

	public void setTopicList(List<TopicInfo> topicList) {
		this.topicList = topicList;
	}

	public String getCourseSubId() {
		return courseSubId;
	}

	public void setCourseSubId(String courseSubId) {
		this.courseSubId = courseSubId;
	}

	public File getCoursePhoto() {
		return coursePhoto;
	}

	public void setCoursePhoto(File coursePhoto) {
		this.coursePhoto = coursePhoto;
	}

	public String getCoursePhotoFileName() {
		return coursePhotoFileName;
	}

	public void setCoursePhotoFileName(String coursePhotoFileName) {
		this.coursePhotoFileName = coursePhotoFileName;
	}

	public String getCoursePhotoContentType() {
		return coursePhotoContentType;
	}

	public void setCoursePhotoContentType(String coursePhotoContentType) {
		this.coursePhotoContentType = coursePhotoContentType;
	}

}
